package com.banciyuan.bcywebview.utils.http;

import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.encrypt.Encrypt;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

//http请求客户端
public class HttpUtils {

    static {
        try {
            System.loadLibrary("random");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static String ENCODENUM;

    private static native String getEncodeNum();

    public native static String getWeibo();

    private native static String getWeixin();

    private native static String getQQ();

    //    public static final String BASE_URL = "http://llapi-test.bcy3.info/api/";
//    public static final String BASE_URL = "https://zzapi.bcy3.info/api/";
//    public static final String BASE_URL = "https://api.bcy.net/api/";
//    public static final String BASE_URL = "https://llapi.bcy3.info/api/";
//    public static final String BASE_URL = "https://lhyapi.bcy3.info/api/";
    public static final String BASE_URL = "https://saboapi.bcy3.info/api/";


    //以下是各个接口和请求的属性名字，有很多是重复的，当时的考虑是根据常量前缀来判断是否引用正确，可以考虑合并
    public static final String UPLOAD_STATUS = "status";
    public static final String UPLOAD_DATA = "data";
    public static final String UPLOAD_ERROR = "error";
    public static final String UPLOAD_SUCCESS = "success";
    public static final String UPLOAD_NODATA = "nodata";
    public static final String UPPULL_SINCE = "since";
    public static final String UPPULL_SOURCE = "source";
    public static final String UPPULL_TYPE_FOLLOW = "follow";
    public static final String UPPULL_TYPE_SQUARE = "square";
    public static final String UPPULL_TYPE_USER = "user";

    public static final String USER_TOKEN = "token";
    public static final String LOGIN_PARAM_USER = "user";
    public static final String LOGIN_PARAM_PASS = "pass";

    public static final String SINA_PARAM_TOKEN = "sina_token";
    public static final String SINA_PARAM_ETIME = "etime";
    public static final String SINA_PARAM_NAME = "sina_name";

    public static final String QQ_PARAM_UID = "qq_unid";
    public static final String QQ_PARAM_TOKEN = "qq_token";
    public static final String QQ_PARAM_NAME = "qq_name";
    public static final String QQ_PARAM_ETIME = "etime";

    public static final String REGISTER_PARAM_EMAIL = "email";
    public static final String REGISTER_PARAM_NICKNAME = "nickname";
    public static final String REGISTER_PARAM_PASS = "password";
    public static final String REGISTER_PARAM_VERIFY = "verify";
    public static final String REGISTER_PARAM_UNIVER = "univer";
    public static final String REGISTER_PARAM_SINANAME = "sina_name";

    public static final String FOLLOW_UIDS = "uids";
    public static final String TYPE = "type";

    public static final String SINA_PARAM_UID = "unid";
    public static final String UID = "uid";
    public static final String FID = "fid";
    public static final String CPID = "cp_id";
    public static final String DPID = "dp_id";
    public static final String RPID = "rp_id";
    public static final String UDID = "ud_id";
    public static final String WPID = "wp_id";
    public static final String TOKEN = "token";
    public static final String GID = "gid";
    public static final String UAID = "ua_id";
    public static final String POSTID = "post_id";
    public static final String EVENT_ID = "event_id";
    public static final String SPECIAL_ID = "special_id";
    public static final String WNS_ID = "wns_id";
    public static final String TAG_ID = "tag_id";
    public static final String COMMENT_ID = "reply_id";
    public static final String ID = "id";
    public static final String ES_ID = "es_id";
    public static final String WID = "wid";
    public static final String INDEX_P = "p";
    public static final String BID = "bid";
    public static final String PATH = "path";
    public static final String OUID = "ouid";
    public static final String LIMIT = "limit";
    public static final String REPLY_ID = "comment_id";

    public static final String COMENT_SUB = "sub";
    public static final String REPLY_PTYPE = "p_type";
    public static final String GROUP_PATHS = "paths";
    public static final String GROUP_INTRO = "intro";
    public static final String GROUP_WORK = "work";
    public static final String DAILY_CONTENT = "content";
    public static final String ASK_ANONYMOUS = "anonymous";
    public static final String TAG_FILTER = "filter";
    public static final String NAME = "name";
    public static final String TAG_NAMED = "tag_name";
    public static final String TAG_NAMES = "tag_names";
    public static final String SEARCH_QUERY = "query";
    public static final String URL_HEAD = "http://";
    public static final String HTTPS_HEAD = "https://";
    public static final String URL = "url";
    public static final String WORK = "work";
    public static final String TAG = "tag";
    public static final String FACE = "face";
    public static final String STYPE = "stype";
    public static final String IS_OPEN = "is_open";
    public static final String SEX = "sex";
    public static final String PROVINCE = "province";
    public static final String CITY = "city";
    public static final String PAGE = "p";
    public static final String RANK_TYPE = "rank_type";
    public static final String AUTHORITY = "authority";
    public static final String MOBILE = "mobile";
    public static final String CODE = "code";
    public static final String DOMAIN = "api.bcy.net";
    public static final String ZGID = "zg_id";
    public static final String ZPID = "zp_id";
    public static final String ZPSID = "zps_id";
    public static final String CART_ID = "cart_id";
    public static final String ADDRESS = "address";
    public static final String SN = "sn";
    public static final String MULTI = "multi";
    public static final String ORDER = "order";


    /**
     * 用来封装和加密post请求中的requestbody
     *
     * @param params
     * @return
     */
    public static HashMap<String, String> getData(List<BcyPair> params) {

        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < params.size(); i++) {
            BcyPair temp = params.get(i);
            try {
                jsonObject.put(temp.getName(), temp.getValue());
            } catch (JSONException e) {
            }
        }
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put(UPLOAD_DATA, Encrypt.getEncryptedParams(jsonObject.toString()));
        return paramMap;
    }


    public static void initInterface() {
        HttpUtils.ENCODENUM = getEncodeNum();
        MyConstants.APP_KEY = getWeibo();
        MyConstants.WEIXIN_APP_ID = getWeixin();
        MyConstants.QQ_APP_ID = getQQ();
    }
}